<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改专题')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-subject-edit" th:object="${cmsSubject}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">标题：</label>
            <div class="col-sm-8">
                <input class="form-control" name="title" th:field="*{title}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">副标题：</label>
            <div class="col-sm-8">
                <input class="form-control" name="subtitle" th:field="*{subtitle}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">专题主图：</label>
            <input name="pic" th:value="*{pic}" type="hidden">
            <div class="col-sm-8">
                <div class="file-loading">
                    <input name="file" id="fileinput-demo-1" type="file" multiple>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">专题轮播图</label>
            <input name="albumPics" th:value="*{albumPics}" type="hidden">
            <div class="col-sm-8">
                <div class="file-loading">
                    <input name="file" id="fileinput-demo-3" type="file" multiple>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">开始时间</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="startTime" th:value="${#dates.format(cmsSubject.startTime, 'yyyy-MM-dd HH:mm:ss')}"
                           class="form-control" placeholder="yyyy-mm-dd hh:ii:ss" type="text">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">结束时间</label>
            <div class="col-sm-8">
                <div class="input-group date">
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    <input name="endTime" th:value="${#dates.format(cmsSubject.endTime, 'yyyy-MM-dd HH:mm:ss')}"
                           class="form-control" placeholder="yyyy-mm-dd hh:ii:ss" type="text">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <textarea class="form-control" name="description">[[*{description}]]</textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">显示状态</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('subject_show_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="showStatus" th:value="${dict.dictValue}"
                           th:field="*{showStatus}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var abnum = [[${cmsSubject.albumPics}]];
    var abnumArra = abnum.split(',');
    var initialPreviewConfig = []
    for (j = 0, len = abnumArra.length; j < len; j++) {
        var obj = {caption: "transport-1.jpg", size: 329892, width: "120px", url: ctx + "common/deleteUpload", key: 1};
        obj.key = j
        initialPreviewConfig[j] = obj;
    }
    var prefix = ctx + "mall/subject";
    $("#form-subject-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-subject-edit').serialize());
        }
    }

    /**
     * 图片上传
     */
    $("#fileinput-demo-1").fileinput({
        'theme': 'explorer-fas',
        'uploadUrl': ctx + 'aliyun/upload',
        autoReplace: true,
        initialPreviewAsData: true,
        overwriteInitial: true,
        initialPreview: [$("input[name='pic']").val()],
        uploadExtraData: { //额外参数，将不同的文件存放不同的目录下
            "baseDir": "advertise"
        }
    }).on("fileuploaded", function (event, data, previewId, index) {//上传成功回调
        $("input[name='pic']").val(data.response.url);
        console.log(data.response.url);
    });

    $("input[name='startTime']").datetimepicker({
        format: "yyyy-mm-dd hh:ii:ss",
        minView: "month",
        autoclose: true
    });

    $("input[name='endTime']").datetimepicker({
        format: "yyyy-mm-dd hh:ii:ss",
        minView: "month",
        autoclose: true
    });


    // /**
    //  * 多张图片上传，逗号分割
    //  */
    // $("#fileinput-demo-2").fileinput({
    //     language: 'zh',                                        //语言
    //     'theme': 'explorer-fas',
    //     'uploadUrl': ctx + 'aliyun/upload',
    //     // deleteUrl: '44444',
    //     overwriteInitial: false,
    //     autoReplace: true,
    //     initialPreviewAsData: true,
    //     initialPreview: abnumArra,//[初始显示图片列表]
    //     layoutTemplates: {
    //         // actionDelete:'', //去除上传预览的缩略图中的删除图标
    //         actionUpload: '',//去除上传预览缩略图中的上传图片；
    //         actionZoom: ''   //去除上传预览缩略图中的查看详情预览的缩略图标。
    //     },
    //     initialPreviewConfig: initialPreviewConfig
    // }).on("fileuploaded", function (event, data, previewId, index) {//上传成功回调
    //     console.log(data.response);
    //     var a = $("input[name='albumPics']").val();
    //     if (a.indexOf(",") != -1) { // 包含
    //         a = a + "," + data.response.url;
    //     } else { // 不包含，第一个
    //         a = data.response.url + "";
    //     }
    //     $("input[name='albumPics']").val(a);
    // });


    $("#fileinput-demo-3").fileinput({
        language: 'zh',                                        //语言
        uploadUrl: ctx + 'aliyun/upload',   //上传地址
        showPreview: true,              //展前预览
        showUpload: false,              //显示上传按钮
        showCaption: false,             //显示文字表述
        uploadAsync: false,                             //false 同步上传，后台用数组接收，true 异步上传，每次上传一个file,会调用多次接口
        removeFromPreviewOnError: true, //当选择的文件不符合规则时，例如不是指定后缀文件、大小超出配置等，选择的文件不会出现在预览框中，只会显示错误信息
        maxFileCount: 6,
        // maxFileSize: 1024 * 10,           //单位为kb，如果为0表示不限制文件大小
        allowedFileExtensions: ["jpg", "jpeg", "gif", "png", "bmp"],
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        overwriteInitial: false,
        initialPreviewAsData: true,
        initialPreview: abnumArra,
        initialPreviewConfig: initialPreviewConfig
    }).on('filepredelete', function (event, key, jqXHR, data) {
        // 移除数组的图片
        console.log('Key = ' + key);
        delete abnumArra[key];
        // abnumArra.remove(key)
        console.log("删除成功")
        console.log(abnumArra)
        let m = 0;
        let str = ""
        for (j = 0, len = abnumArra.length; j < len; j++) {
            if (abnumArra[j] === undefined) {
            } else {
                if (m = 0) {
                    str = abnumArra[j] + "";
                } else {
                    str = str + "," + abnumArra[j];
                }
                m = m + 1
            }
        }
        $("input[name='albumPics']").val("");
        $("input[name='albumPics']").val(str);
    }).on("fileuploaded", function (event, data, previewId, index) {//上传成功回调
        abnumArra.push(data.response.url)
        console.log("添加成功")
        console.log(abnumArra)
        let m = 0;
        let str = ""
        for (j = 0, len = abnumArra.length; j < len; j++) {
            if (abnumArra[j] === undefined) {
            } else {
                if (m = 0) {
                    str = abnumArra[j] + "";
                } else {
                    str = str + "," + abnumArra[j];
                }
                m = m + 1
            }
        }
        $("input[name='albumPics']").val("");
        $("input[name='albumPics']").val(str);
    });

</script>
</body>
</html>
